<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class Tab4Category extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {

        $response = [];
        $upgradeVersion = '5.21.0';
        foreach ($hit as $k => $v) {
            $pic_ids = json_decode($v['pic_id'], true);
            $title = $pic_ids['title'] ?? '';
            $icon_id = $pic_ids['icon_id'] ?? '';
            $row = [
                'id' => intval($v['id']),
                'title' => $title,
                'index' => intval($v['pos']),
                'banner' => empty($icon_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $icon_id, ImgUrl::IMAGE_SIZE_OP_ORI),
                'link' => $this->generateLink($v['link_type'], $v['link_params']),
                'statSign' => $this->statSign($v, [], ['title' => $title, 'link' => $this->generateLink($v['link_type'], $v['link_params'])]),
            ];
            $response[] = $row;
        }

        if ($response) {
            $index = array_column($response, 'index');
            array_multisort($index, SORT_ASC, $response);
        }

        return \Doraemon\tools\StatSign::versionCompatible($response, $upgradeVersion, BlockType::TYPE_BANNER, 'banner');
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        $pic_ids = json_decode($v['pic_id'], true);
        $v['title'] = $pic_ids['title'] ?? '';
        $v['icon_url'] = empty($pic_ids['icon_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['icon_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
        $v['ext_icon_url'] = empty($pic_ids['small_icon_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['small_icon_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
    }

    public function getExtInfo($baseInfo)
    {
        $pic_id = json_decode($baseInfo['pic_id'], true);
        return [
            'title' => $pic_id['title'] ?? '',
            'icon_id' => $pic_id['icon_id'] ?? '',
            'small_icon_id' => $pic_id['small_icon_id'] ?? '',
        ];
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }
}